var Modifier = function(mesh){
	this.mesh = mesh;
	this.gui;
	this.dataStack = [];//存储修改前的值
	this.nameStack = [];//存储修改器添加列表
	this.name;
	this.pickedObject = null;
	this.toPick = false;
}

var modifiers = {
	
}

Modifier.prototype.toMesh = function(){
	this.gui = new dat.GUI({autoPlace:false});
	var scope = this;
	this.name = "网格编辑器";
	this.nameStack.push("网格编辑器");
	var points = this.mesh.geometry.vertices;
	var faces = this.mesh.geometry.faces;

	var flag = true;//控制只能点击一次
	var tempVector = new THREE.Vector4( 0, 0, 0, 1 );
	this.mesh.controlGeometry = [];
	this.mesh.modifier = this;

	var levels = {
		pointLevel:function(){
			if(flag){
				scope.mesh.isPointLevel = true;
			for(index in points){
				var pointHelper = new THREE.Mesh(new THREE.SphereGeometry(1),
				new THREE.MeshBasicMaterial( {color:0xff0000}));
				scene.add(pointHelper);
			
				pointHelper.type = 'helper';
				pointHelper.targetObject = this.mesh; 
				tempVector.x = points[index].x;
				tempVector.y = points[index].y;
				tempVector.z = points[index].z;
				tempVector.applyMatrix4 (scope.mesh.matrix);
				
				pointHelper.position.x = tempVector.x;
				pointHelper.position.y = tempVector.y;
				pointHelper.position.z = tempVector.z;
				
				scope.mesh.controlGeometry.push(pointHelper);
	
			}
			// gui.add(levels,'');
			flag = false;
			}
			
		},
		faceLevel:function(){
			for(index in faces){

			}
		}
	}
		
		this.gui.add(levels,'pointLevel');
		this.gui.add(levels,'faceLevel');
		propertyPanel.update();

	}


Modifier.prototype.booleanOperation = function(){
	var scope = this;
	this.gui = new dat.GUI({autoPlace:false});
	this.gui.domElement.appendChild(uiMaker());
	
	this.mesh.modifier = this;
	this.name = "布尔运算";
	this.nameStack.push("布尔运算");

	var operationType = null;
	propertyPanel.update();





function uiMaker(){
	var container = document.createElement("div");

	var buttons = [];
	var button_names = ["union","intersect","subtract"]; 

	for(var i = 0; i < button_names.length;i++){
		var button = document.createElement("img");
		buttons.push(button); 
	}

	for(i = 0;i < button_names.length;i++){
		buttons[i].src = "images/" + button_names[i] + ".png";
		container.appendChild(buttons[i]);
	}

	//这里是一个闭包，参考:
	//http://blog.csdn.net/zhaoxuejie/article/details/7827799
	for(i = 0;i < button_names.length;i++){
		(function(){      
		    var p = i ; 
		    buttons[p].addEventListener("mouseover", function(evt){
		    	if(this.src.indexOf("images/" + button_names[p] + ".png") >= 0){
		    		this.src = "images/" + button_names[p] + "_h.png";
		    	}
		    	
		    });  
		    buttons[p].addEventListener("mouseout", function(evt){
		    	if(this.src.indexOf("images/" + button_names[p] + "_h.png") >= 0)
		    	this.src = "images/" + button_names[p] + ".png";
		    }); 
		    buttons[p].addEventListener("click", function(evt){
		    	if(this.src.indexOf("images/" + button_names[p] + "_v.png") >= 0){
		    		this.src = "images/" + button_names[p] + ".png";
		    		operationType = null;
		    		return;
		    	}
		    	for(var j = 0;j < button_names.length;j++){
					buttons[j].src = "images/" + button_names[j] + ".png";
				}

		    	this.src = "images/" + button_names[p] + "_v.png";
		    	operationType = button_names[p];
		    });
		  })();
	}

	var pick_button = addButton("pick");
	// pick_button.id = "pickStatus";
	// var calculateTarget = activeObject;
	// scope.pickedObject = null;
	pick_button.addEventListener("click", function(evt){
			if(this.src.indexOf("images/pick_h.png") >= 0 || this.src.indexOf("images/pick.png") >= 0){
				this.src = "images/pick_v.png";
				// calculateTarget = activeObject;
				scope.toPick = true;
			}
			else{
				this.src = "images/pick.png";
				scope.toPick = false;
			}
		}); 

	//为运算按钮 添加监听  调用相应的运算函数，实现布尔运算
	var operate_button = addButton("operate");

	operate_button.addEventListener("click", function(){
		if(scope.pickedObject == null){
			alert("请先【拾取】参与运算的物体");
			return;
		}
		if(operationType == null){
			alert("请指定运算方式");
			return;
		}
		var spinner = showSpin();
	
		setTimeout(function(){
// --------------------------------------------
//-----------------------&&&&&&&&&&&&&&&&&&----
//-----------------------&&&&&&&&&&&&&&&&&&----
// ----------------------&&&&------------------
// ----------------------&&&&------------------
// ----------------------&&&&------------------
// ----------------------&&&&------------------
// ----------------------&&&&------------------
// ----------------------&&&&------------------
//-----------------------&&&&&&&&&&&&&&&&&&----
//-----------------------&&&&&&&&&&&&&&&&&&----
// --------------------------------------------
			var aBsp = new ThreeBSP(activeObject);
			var bBsp = new ThreeBSP(scope.pickedObject);
			var result;
			var resultBSP;
			switch (operationType) {
				case "union":
					// statements_1
					// console.log('求并');
					resultBSP = aBsp.union(bBsp);
					break;
				case "intersect":
					// statements_1
					// console.log('求交');
					resultBSP = aBsp.intersect(bBsp);
					break;
				case "subtract":
					// statements_1
					// console.log('求差');
					resultBSP = aBsp.subtract(bBsp);
					break;
				default:
					// statements_def
					break;
		};
		console.log(resultBSP);
		result = resultBSP.toMesh();
		result.material = activeObject.material;
		result.geometry.computeFaceNormals();
		result.geometry.computeVertexNormals();
		scene.add(result);
		meshFixed(result);
			spinner.stop();
		},200); 
		
	});
	function meshFixed(mesh){
		control.attach(mesh);
		var gui = new dat.GUI({autoPlace: false});
		mesh.gui = gui;
		mesh.name = activeObject.name + "布尔运算";
		// console.log(mesh.id);
		// console.log(activeObject.id);
		manager.cloneMaterialModifier(activeObject,mesh);
		activeObject = mesh; 
		outliner.addOutlinerItem(activeObject.name,activeObject.id);
		manager.buildGeometryGeneral(activeObject);
		propertyPanel.update();
		materialPanel.update();
	}
	var label = document.createElement("label");
	var text = document.createElement("sapn");
	text.style.fontSize = "16px";
	// text.style.color = "#ffffff";
	text.style.textShadow = "none";
	text.style.fontFamily = "黑体";
	text.innerHTML = "拾取对象:";

	// label.readonly = "readonly";
	label.style.backgroundColor = "#ffffff";
	label.style.paddingBottom = "3px";
	label.style.paddingLeft = "5px";
	label.style.paddingRight = "5px";
	label.style.paddingTop = "3px";
	label.style.fontSize = "12px";
	label.style.textShadow = "none";
	label.style.color = "#000000";
	// label.width = "200px";
	label.innerHTML = "空";
	label.id = "picked_Object";


	container.appendChild(text);
	container.appendChild(label);

	return container;
	//为按钮添加监听器，来控制鼠标点击事件时的图片更换，并且添加到容器(container)中
	function addButton (name) {
		var button = document.createElement("img");
		button.src = "images/" + name + ".png";

		button.addEventListener("mouseover", function(evt){
			if(button.src.indexOf("images/" + name + ".png") >= 0 )
				button.src = "images/" + name + "_h.png";
		});

		button.addEventListener("mouseout", function(evt){
			if(button.src.indexOf("images/" + name + "_h.png") >= 0)
				button.src = "images/" + name + ".png";
		});
		container.appendChild(button);
		return button;
	}
	//加载动画
	function showSpin(){
  		var opts = {
	  lines: 9, // The number of lines to draw
	  length: 0, // The length of each line
	  width: 5, // The line thickness
	  radius: 11, // The radius of the inner circle
	  corners: 1, // Corner roundness (0..1)
	  rotate: 0, // The rotation offset
	  color: '#000', // #rgb or #rrggbb
	  speed: 1.5, // Rounds per second
	  trail: 42, // Afterglow percentage
	  shadow: false, // Whether to render a shadow
	  hwaccel: false, // Whether to use hardware acceleration
	  className: 'spinner', // The CSS class to assign to the spinner
	  zIndex: 2e9, // The z-index (defaults to 2000000000)
	  top: '50%', // Top position relative to parent in px
	  left: '40%', // Left position relative to parent in px
	  position: 'absolute'
	};
		var target = document.getElementById('canvas-frame');
		// console.log(target);

		var spinner = new Spinner(opts).spin(target);
		return spinner;
  	}
}

	function calculate(operationType){

	}
}

Modifier.prototype.setTargetObject = function(mesh){
	this.pickedObject = mesh;
	document.getElementById("picked_Object").innerHTML = mesh.name;
}

